import {DateUtils} from '../../utils/Utils';
import Constants from '../../common/Constants';
import ToastUtils from '../../common/ToastUtils';
import DataManager from '../DataManager';

import IMSingleLinker from './IMSingleLinker';

//单个聊天的链接
export default class QZ_QunliaoChatLinker extends IMSingleLinker {
  constructor(props) {
    super(props);

    this._currentDatas = null;
    this._dataPage = 1;
    this._dataKey = "id";
    this._needSort = false;
  }

  getDatas() {
    return this._currentDatas;
  }

  init(params) {
    super.init(params);
    this._dataPage = 1;
    this._currentDatas = this._allDatas.slice(-this.PAGE_SIZE * this._dataPage);
    this._currentDatas.unshift({dataType:"top"});

    this._groupData = params.groupData;
    DataManager.modelDic[Constants.MODULE_TYPE.IM].bindGroup(this._groupData.im_group_id, this._modelUpdateCallback.bind(this));
  }

  dispose() {
    DataManager.modelDic[Constants.MODULE_TYPE.IM].unBindGroup(this._groupData.im_group_id);
    this._currentDatas = null;
  }

  _modelUpdateCallback(result, data) {
    // ToastUtils.show("modelUpdateCallback:" + this._groupData.im_group_id + "---" + data.length + "===" + JSON.stringify(data));
    this.requestFinish(data, result);
  }

  _dealSendMessage(message) {
    DataManager.modelDic[Constants.MODULE_TYPE.IM].sendGroupText({msg:message, to:this._groupData.im_group_id},
      this.sendMessageFinish.bind(this));
  }

  _dealRequestData() {
    this.requestFinish([], true);
  }

  _dealDatas(data) {
    super._dealDatas(data);
    if(this._isLoadMore) {
      this._dataPage += 1;
    }
    this._currentDatas = this._allDatas.slice(-this.PAGE_SIZE * this._dataPage);
    if(this._currentDatas.length < this.PAGE_SIZE * this._dataPage) {
      this._dataPage = parseInt(this._currentDatas.length / this.PAGE_SIZE);
      let less = this._currentDatas.length % this.PAGE_SIZE;
      if(less > 0) {
        this._dataPage += 1;
      }
    }
    this._currentDatas.unshift({dataType:"top"});
    console.log("_dealDatas", this._currentDatas, this._dataPage);
  }

  _sendUpdateCall(result, isLoadMore) {
    this._updateCall(this._currentDatas, result, isLoadMore);
  }
}
